package com.zc.blog.controller;

import com.zc.blog.model.Article;
import com.zc.blog.model.Tag;
import com.zc.blog.service.IArticleService;
import com.zc.blog.service.ITagService;
import com.zc.blog.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
@RequestMapping("/tag")
public class TagController {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private ITagService tagService;

    @Value("${pageSize}")
    private int pageSize;

    /**
     * 根据标签Id获取文章
     * @param tagId 标签id
     */
    @RequestMapping("/{tagId}")
    public String galleryPost(@PathVariable("tagId") Integer tagId, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "query",required = false) String query, Model model) {
        Tag tag = tagService.findById(tagId); //标签实体
        List<Article> tagArticlesList = articleService.listTagArticles(page,pageSize,query,tagId); //根据标签获取文章
        int total = articleService.getTagArticlesTotal(query,tagId);// 获取符合查询条件的文章总数
        String pageCode= PageUtil.genPagation("/tag/"+tagId, total, page, pageSize,query); // 生成分页链接

        model.addAttribute("tag", tag); //标签实体
        model.addAttribute("tagArticlesList", tagArticlesList);  //标签文章列表
        model.addAttribute("pageCode", pageCode);  //分页链接
        model.addAttribute("query", query);        //查询参数
        return "blog/articleTag";
    }

}
